from data_type import EmailTemplate
from data_type import EmailTemplateAbstract

_email_template_store = {
  'dummy9000': EmailTemplate({
    'key': 'dummy9000',
    'description': 'This is just a dummy template for testing.',
    'to': {'dummy@xcorp.com'},
    'cc': {'the_coworker.xcorp.com'},
    'subject': '{{topic}}',
    'body': '''Hello
      {{topic}}

      To inform you of:
      {{information}}'''
  }),
}

_ABSTRACT_KEY = {'key', 'description'}


def GetTemplate(key):
  template = _email_template_store.get(key, None)
  return template


def GetTemplates():
  abstract_list = []
  for template in _email_template_store.values():
    abstract_list.append(EmailTemplateAbstract({
      'key': template['key'][:],
      'description': template['description'][:]
    }))

  return abstract_list


def CreateTemplate(template):
  _email_template_store[template['key']] = template


def UpdateTemplate(template):
  if template['key'] in _email_template_store:
    _email_template_store[template['key']] = template
  else:
    raise KeyError('EmailTemplate not found with key: %s' % template['key'])


def DeleteTemplate(key):
  if not key in _email_template_store:
    raise KeyError('No template with key "%s".' % key)
  del _email_template_store[key]
